package com.tiger.smartapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

public class BaseActivity extends Activity{
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
        	this.finish();
        	}
        return false;
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(1,1,1,"退出");
		menu.add(2,2,2,"分享");
		menu.add(3,3,3,"关于");
		return super.onCreateOptionsMenu(menu);
	}
	public void AboutUS() {
		// 关于
		String about_appname = this.getResources().getString(
				R.string.app_name);
		String about_ver = "1.0版";
		String about_csmail = "tiger.chenlin@gmail.com";
		String about_copyright = "(c)2010-2011\ntiger 保留所有权";
		String app_platform = "Android1.5+";

		CommonUtil.alert(this,about_appname + "(" + app_platform + ")" + "\n版本:" + about_ver
				+ "\n客服邮箱:" + about_csmail + "\n版权信息:" + about_copyright + "",
				"关于");
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case 1:
			confirm("确认退出吗?","退出");
			break;
case 2:
	shareWith("智能程序管理下载地址：http://bbs.hiapk.com/attachment.php?aid=974043&k=9fb7868e9b578aab4214cc4a9e67fb7c&t=1304671170&ck=cc2f515c&sid=0b2ecyfjUlmn4G3ip%2FskPZ%2BoHeS3%2FrNOKyXGMYmcNDT2Zp8");
			break;
case 3:
	AboutUS();
	break;
		}
		return super.onOptionsItemSelected(item);
	}
	public void confirm(String msg,String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg);
		builder.setTitle(title);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
//				 ActivityManager activityMgr=(ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
//			     activityMgr.restartPackage(getPackageName());
				Intent startMain = new Intent(Intent.ACTION_MAIN); 
				startMain.addCategory(Intent.CATEGORY_HOME); 
				startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
				startActivity(startMain); 
				System.exit(0); 
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.dismiss();
			}
		});
		builder.show();
	}
	public void shareWith(String text){
//		Intent intent=new Intent(Intent.ACTION_SEND);
//	      intent.setType("text/plain");
//	      intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
//	      intent.putExtra(Intent.EXTRA_TEXT, "I would like to share this with you...");
//	      startActivity(Intent.createChooser(intent, getTitle()));
		Intent it = new Intent(Intent.ACTION_SEND);
		it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		it.putExtra(Intent.EXTRA_TEXT, text);
		it.setType("text/plain");
		Intent newIntent = Intent.createChooser(it, "请选择");
		newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(newIntent);

	}
}
